$(function () {
    var netModel = new NetModel();
    var editor;
    var modelDialog;
    var changed;

    function setMarkdown(md) {
        if (editor == null) {
            editor = editormd('editormd', {
                id: 'editormd',
                height: 'calc(100%)',
                tocm: true,
                // tocContainer: '#tocm',
                // tocDropdown: true,
                path: 'vendor/editormd/lib/',
                theme: 'dark',
                previewTheme: 'dark',
                editorTheme: 'mbo',
                appendMarkdown: md,
                onchange: onMarkdownChanged
            });
        } else {
            editor.setMarkdown(md);
        }

        setInterval(function () {
            if (changed) {
                netModel.http({
                    url: '/note/save',
                    data: { 'data': editor.getMarkdown() },
                    success: function (result) {
                        console.log(JSON.stringify(result));
                    }
                });
                changed = false;
            }
        }, 2048);
    }

    function onMarkdownChanged() {
        changed = true;
    }

    var vue = new Vue({
        el: '#vue-app',
        data: {
            userName: 'Lambda',
            workDir: {},
        },
        methods: {
            showAddDialog: function () {
                modelDialog.css('display', 'block');
            },
            closeAddDialog: function () {
                modelDialog.css('display', 'none');
            },
            addNoteCate: function () {
                var fileName = $('#text').val();
                if (fileName.length <= 0) {
                    $('#text').focus();
                    Hub.warn('text bbbbbbbbbb');
                    return;
                }

                var type = $('#r1')[0].checked ? 0 : 1;

                netModel.http({
                    url: '/note/addCateOrNote',
                    data: { 'name': fileName, 'type': type },
                    success: function (result) {
                        if (result.code == 1) {
                            var name = type == 0 ? fileName : fileName + '.md';
                            vue.workDir.files.push(name);
                            vue.closeAddDialog();
                        } else {
                            console.log(result.msg);
                        }
                    }
                });
            },
            throwTrash: function (pos) {
                netModel.http({
                    url: '/note/throwTrash',
                    data: { 'name': vue.workDir.files[pos] },
                    success: function (result) {
                        if (result.code == 1) {
                            vue.workDir.files.splice(pos, 1)
                        } else {
                            console.log(result.msg);
                        }
                    }
                });
            },
            loadNote: function (event) {
                netModel.http({
                    url: '/note/open',
                    data: { 'name': event.target.innerText },
                    success: function (result) {
                        if (result.code == 1) {
                            if (typeof result.data == 'string') {
                                setMarkdown(result.data);
                            } else {
                                Vue.set(vue, 'workDir', result.data);
                            }
                        } else {
                            console.log(result.msg);
                        }
                    }
                });
            },
            returnDir: function () {
                netModel.http({
                    url: '/note/backDir/',
                    success: function (result) {
                        if (result.code == 1) {
                            Vue.set(vue, 'workDir', result.data);
                        } else {
                            console.log(result.msg);
                        }
                    }
                });
            }
        },
        mounted: function () {
            modelDialog = $('.model');

            netModel.http({
                url: '/note/notes',
                success: function (result) {
                    if (result.code == 1) {
                        Vue.set(vue, 'workDir', result.data);
                    } else if (result.code == -110) {
                        window.location.href = 'login.html';
                    } else {
                        console.log(result.msg);
                    }
                }
            });
        }
    })
});